package vn.vos.ui;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import vn.vos.R;
import vn.vos.entity.Point;
import vn.vos.entity.Road;
import vn.vos.manager.RoadProvider;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MapRouteActivity extends MapActivity implements OnClickListener {
	LinearLayout linearLayout;
	MapView mapView;
	private Road mRoad;
	TextView fromText;
	TextView toText;
	
	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {			
			MapOverlay mapOverlay = new MapOverlay(mRoad, mapView);
			List<Overlay> listOfOverlays = mapView.getOverlays();
			listOfOverlays.clear();
			listOfOverlays.add(mapOverlay);
			mapView.invalidate();
		};
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		Button find = (Button) findViewById(R.id.button6);
		find.setOnClickListener(this);				
	}	

	private InputStream getConnection(String url) {
		InputStream is = null;
		try {
			URLConnection conn = new URL(url).openConnection();
			is = conn.getInputStream();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return is;
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public void onClick(View v) {
		try {
			/**determine the start to finish point*/
			fromText = (TextView) findViewById(R.id.editText1);
			toText = (TextView) findViewById(R.id.editText2);
			String from = fromText.getText().toString();
			String to = toText.getText().toString();
			final List<Point> fromPoints = RoadProvider.getLocByAdd(from, new Geocoder(this));
			final List<Point> toPoints = RoadProvider.getLocByAdd(to, new Geocoder(this));
			
			
			/**drawing with a new thread*/
			new Thread() {
				@Override
				public void run() {
					double fromLat = 0.0;
					double fromLon = 0.0;
					double toLat = 0.0;
					double toLon = 0.0;
					
					for(int i = 0, j = 0; i < fromPoints.size() && j < toPoints.size();i++, j++){
						fromLat = fromPoints.get(i).getLatitude();
						fromLon = fromPoints.get(i).getLongitude();
						toLat = toPoints.get(j).getLatitude();
						toLon = toPoints.get(j).getLongitude();
						
						String url = RoadProvider
								.getUrl(fromLat, fromLon, toLat, toLon);
						InputStream is = getConnection(url);
						mRoad = RoadProvider.getRoute(is);
						mHandler.sendEmptyMessage(0);
					}					
				}
			}.start();
		} catch(Exception ex) {
			Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG);
		}
	}
}

class MapOverlay extends com.google.android.maps.Overlay {
	Road mRoad;
	ArrayList<GeoPoint> mPoints;

	public MapOverlay(Road road, MapView mv) {
		mRoad = road;
		if (road.mRoute.length > 0) {
			mPoints = new ArrayList<GeoPoint>();
			for (int i = 0; i < road.mRoute.length; i++) {
				mPoints.add(new GeoPoint((int) (road.mRoute[i][1] * 1000000),
						(int) (road.mRoute[i][0] * 1000000)));
			}
			int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
					mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
					.getLatitudeE6()) / 2);
			int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
					mPoints.size() - 1).getLongitudeE6() - mPoints.get(0)
					.getLongitudeE6()) / 2);
			GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);

			MapController mapController = mv.getController();
			mapController.animateTo(moveTo);
			mapController.setZoom(7);
		}
	}

	@Override
	public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
		super.draw(canvas, mv, shadow);
		drawPath(mv, canvas);
		return true;
	}

	public void drawPath(MapView mv, Canvas canvas) {
		int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
		Paint paint = new Paint();
		paint.setColor(Color.GREEN);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(3);
		for (int i = 0; i < mPoints.size(); i++) {
			android.graphics.Point point = new android.graphics.Point();
			mv.getProjection().toPixels(mPoints.get(i), point);
			x2 = point.x;
			y2 = point.y;
			if (i > 0) {
				canvas.drawLine(x1, y1, x2, y2, paint);
			}
			x1 = x2;
			y1 = y2;
		}
	}
}